//
//  Created by Rene Dohan on 11/12/11.
//
// To change the template use AppCode | Preferences | File Templates.
//


#import "Application.h"

static NSString *const USERNAME = @"username";
static NSString *const PASSWORD = @"password";
static NSString *const URL = @"url";

@implementation Application

static Application *application;
@synthesize session, userType;


+ (Application *)getInstance {
    if (application == nil) {
        application = [Application new];
    }
    return application;
}

- (id)init {
    save = [NSUserDefaults standardUserDefaults];
    return self;
}

- (NSString *)forceString:(NSString *)string {
    if (string) return string;
    return @"";
}

- (NSString *)username {
    return [self forceString:[save stringForKey:USERNAME]];
}

- (NSString *)password {
    return [self forceString:[save stringForKey:PASSWORD]];
}

- (BOOL)isSetup {
    return self.url.length != 0;
}

- (BOOL)isMerchant {
    return [self.userType isEqual:@"merchant"];
}

- (BOOL)isLogged {
    return self.userType != NULL;
}

- (NSString *)url {
    return [self forceString:[save stringForKey:URL]];
}

- (void)save:(NSString *)url:(NSString *)username :(NSString *)password {
    [save setObject:url forKey:URL];
    [save setObject:username forKey:USERNAME];
    [save setObject:password forKey:PASSWORD];
    [save synchronize];
}

- (void)onLogin:(NSString *)url :(NSString *)username :(NSString *)password :(NSString *)newSession :(NSString *)newUserType {
    [self save:url :username :password];
    self.session = newSession;
    self.userType = newUserType;
}


@end